package com.beep.erp.model.goodsdetail;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.CoordinatorLayout;
import android.support.v7.widget.RecyclerView;
import android.widget.TextView;

import com.beep.erp.Constants;
import com.beep.erp.R;
import com.beep.erp.common.base.ToolbarActivity;
import com.beep.erp.common.bean.GoodsDetailBean;
import com.beep.erp.common.utils.MD5util;
import com.beep.erp.common.utils.TimeUtils;
import com.beep.erp.common.utils.ToastUtils;
import com.beep.erp.common.utils.ToolsUtils;
import com.beep.erp.common.widget.refresh.RefreshView;
import com.beep.erp.common.widget.tips.TipsHelper;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 入库单详情页
 */
public class GoodsDetailActivity extends ToolbarActivity<GoodsDetailPresenter, GoodsDetailModel> implements GoodsDetailContract.View, RefreshView.OnFreshListener {
    private static final String GOODSID = "goodsId:";
    @BindView(R.id.appbatlayout)
    AppBarLayout appbatlayout;
    @BindView(R.id.super_refresh)
    RefreshView refreshView;
    @BindView(R.id.activity_invite)
    CoordinatorLayout activityInvite;
    @BindView(R.id.tv_goods_name)
    TextView tvGoodsName;
    @BindView(R.id.tv_goods_code)
    TextView tvGoodsCode;
    @BindView(R.id.tv_goods_specification)
    TextView tvGoodsSpecification;
    @BindView(R.id.tv_goods_stockAmountn)
    TextView tvGoodsStockAmountn;
    @BindView(R.id.tv_goods_purchasingAmount)
    TextView tvGoodsPurchasingAmount;
    @BindView(R.id.tv_goods_goodsBrandName)
    TextView tvGoodsGoodsBrandName;
    @BindView(R.id.tv_goods_goodsCategoryName)
    TextView tvGoodsGoodsCategoryName;
    @BindView(R.id.tv_goods_supplierName)
    TextView tvGoodsSupplierName;

    private TipsHelper mTipsHelper;

    public static void start(Context context, String stockId) {
        Intent intent = new Intent(context, GoodsDetailActivity.class);
        intent.putExtra(GOODSID, stockId);
        context.startActivity(intent);
    }

    @Override
    protected boolean isBack() {
        return true;
    }

    @Override
    protected int getLayout() {
        return R.layout.activity_goods_detail;
    }

    @Override
    protected void initView() {
        getTvTitle().setText(R.string.goods_detail);
        mTipsHelper = createTipsHelper(refreshView);

    }

    @Override
    public void getDate() {
        //请求数据
        getStockDetail();
    }

    @Override
    public void getGoodsDetail(GoodsDetailBean goodsDetailBean) {
        tvGoodsName.setText(goodsDetailBean.getGoodsName());
        tvGoodsCode.setText(goodsDetailBean.getGoodsCode());
        tvGoodsSpecification.setText(goodsDetailBean.getSpecification());
        tvGoodsStockAmountn.setText(goodsDetailBean.getStockAmount() + "");
        tvGoodsPurchasingAmount.setText(goodsDetailBean.getPurchasingAmount());
        tvGoodsGoodsBrandName.setText(goodsDetailBean.getGoodsBrandName());
        tvGoodsGoodsCategoryName.setText(goodsDetailBean.getGoodsCategoryName());
        tvGoodsSupplierName.setText(goodsDetailBean.getSupplierName());
    }

    private void getStockDetail() {
        long mills = TimeUtils.getCurTimeMills();

        Map<String, String> map = new HashMap<>();
        map.put("goodsId", getIntent().getStringExtra(GOODSID));
        mPresenter.getGoodsDetail(MD5util.MD5Encode(ToolsUtils.buildSignStr(map, mills)), mills + "", getIntent().getStringExtra(GOODSID));
    }

    @Override
    public void showLoading() {
    }

    @Override
    public void hideLoading() {
        refreshView.onFinishFreshAndLoad();
    }

    @Override
    public void showMessage(String message) {
        //错误信息
        if (message.equals(getString(R.string.connection_timed_out)) || message.equals(getString(R.string.error)) || message.equals(getString(R.string.network_error)) || message.equals(getString(R.string.connection_failed)) || message.equals(getString(R.string.certificate_validation_failed))) {
            mTipsHelper.showError(true, message, v -> getDate());
        } else {
            ToastUtils.showShort(message);
        }
    }

    //刷新
    @Override
    public void onRefresh() {
        getDate();
    }

    @Override
    public void onLoadmore() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}
